/*
 * Creator:     WRX
 * CreatDate:   2022-09-8
 * descripts：  视光 - 库存 - 调价单生效【弃用】
 * */
import React, {Component} from 'react';
import moment from "moment";
import {$http} from "../../../containers/config/https";
import {Button, Card, Icon, Input, message, Radio} from "antd";
import RangeDatePicker from "../../../components/rangePicker";
import PubilcTablePagination from "../../common/PubilcTablePagination";
import store from 'store';

class PriceAdjustBillEffect extends Component {
  constructor(props) {
    super(props);
    this.userData = React.$getSessionData();
    this.patData = React.$getSessionData('titleData');
    this.state = {
      page: 1,
      pageSize: 20,
      total: 0,// 预约人数
      startDate: moment(),// 开始日期
      endDate: moment(),// 结束日期
      tableData: [],
      columns: [],
      totalWidth: '100%',
      loading: false,
      detailSelectedRowKeys: [],
      adjIDs: [],
      stockAlias: '',
      status: 'No'
    }
  }

  componentDidMount() {
    this.getTableColumns(); // 获取列表头数据 -- 01040073
  }

  // +----------------接口相关---------------+

  // 获取列表头数据 -- 01040073
  getTableColumns = async () => {
    let operationObj = {
      width: 80,
      title: '操作',
      fixed: 'right',
      align: 'center',
      key: 'operation',
      render: (text, record) => (
        <Button type="link" style={{color:record.stateDesc === '已生效'?"grey":"rgb(24, 141, 244)"}} size="small"
                disabled={record.stateDesc === '已生效'}
                onClick={() => {
                  this.priceEffectHandle(record.adjID)
                }}
        >
          立即生效
        </Button>
      ),
    };
    try {
      let data = {
        params: [{
          compontName: 'PriceAdjustBillEffect',
          type: 'C',
        }]
      }
      let res = await React.$asyncPost(this, '01040073', data);
      let filterArr = res.result && res.result.C && res.result.C.length > 0 ? res.result.C : [];
      if (filterArr && filterArr.length > 0) {
        filterArr = filterArr && filterArr.length > 0 ? [...filterArr, operationObj] : []
        this.setState({
          columns: filterArr,
          totalWidth: res.totalWidth
        })
      }
    } catch (error) {
      console.log(error)
    }
  }

  // +---------------查询条件相关-----------------+
  // 开始时间更换
  startDateChange = (e) => {
    this.setState({startDate: moment(e)})
  }

  // 结束时间更换
  endDateChange = (e) => {
    this.setState({endDate: moment(e)})
  }

  // 生效状态
  onChange = (checkedValues) => {
    this.setState({status: checkedValues.target.value, detailSelectedRowKeys: [], adjIDs: []})
  }

  // table复选项
  handleDetailSelectedRowKeysChange = (selectedRowKeys, selectedRows) => {
    let objArr = []
    let massArr = []
    objArr = selectedRows
    objArr.map(item => {
      massArr.push({adjID: item.adjID})
    })
    this.setState({
      detailSelectedRowKeys: selectedRowKeys,
      adjIDs: massArr
    })
  }

  // 行类名
  setRowClassNamePublic = (record) => {
    let rowStyle = ''
    let rowIdArr = []
    rowIdArr = this.state.adjIDs
    rowIdArr.map(item => {
      if (item.adjID === record.adjID) {
        rowStyle = 'clickRowStyle'
      }
    })
    return rowStyle
  }

  // 分页操作
  compilePage = (page, pageSize) => {
    this.setState({page, pageSize}, () => {
      //this.appointmentListQuery();
    })
  }

  // 调价审核生效 -- 17033008
  priceEffectHandle = (adjID) => {
    const {startDate, endDate} = this.state
    let data = {
      params: [
        {
          adjID
        }
      ],
      searchParams: [
        {
          startDate: startDate,
          endDate: endDate,
          state: "Audit"
        }
      ]
    }
    $http.post('urlS', this, {
      code: '17033010',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          message.success('审核成功')
          this.priceAdjustReviewQuery()
        }
      }
    })
  };

  // 调价批量审核生效 -- 17033008
  priceEffectHandleMass = () => {
    const {startDate, endDate, adjIDs} = this.state
    let data = {
      params: adjIDs,
      searchParams: [
        {
          startDate: startDate,
          endDate: endDate,
          state: "Audit"
        }
      ]
    }
    $http.post('urlS', this, {
      code: '17033010',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          message.success('审核成功')
          this.priceAdjustReviewQuery()
        }
      }
    })
  };

  // 调价单审核列表 -- 17033004
  priceAdjustReviewQuery = () => {
    let data = {
      params: [
        {
          stockAlias: this.state.stockAlias,
          startDate: this.state.startDate.format('YYYY-MM-DD'),  //查询开始日期
          endDate: this.state.endDate.format('YYYY-MM-DD'),    //查询结束日期
          state: this.state.status,   //关键词
        }
      ],
      pagination: [
        {
          pageSize: this.state.pageSize,   //页面大小
          currentPage: this.state.page, //当前页
        }
      ]
    };
    $http.post('urlS', this, {
      code: '17033004',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          this.setState({tableData: res.result, total: res.result.total})
        }
      }
    })
  };

  render() {
    const {total, tableData, columns, totalWidth, page, loading, startDate, endDate, detailSelectedRowKeys} = this.state;
    const options = [
      {label: '未生效', value: 'Audit'},
      {label: '已生效', value: 'Yes'}
    ];
    const detailRowSelection = {
      selectedRowKeys: detailSelectedRowKeys,
      onChange: this.handleDetailSelectedRowKeysChange
    };
    return (
      <div className="price-adjust-bill-review" style={{background: 'rgb(245,247,254)'}}>
        <Card title={<div><Icon type="minus-square" theme="twoTone" style={{marginRight: 5}}/>查询条件</div>}
              bordered={true} size='small'
              bodyStyle={{padding: 20}}
              style={{marginBottom: 10}}
        >
          <div className="gmaf-header flex-wrap-align-items">
            <div style={{marginRight: 15}}>
              <RangeDatePicker
                width="300px"
                label="日期："
                isChange={true}
                format='YYYY-MM-DD'
                className='express-select-border'
                startDate={startDate}
                endDate={endDate}
                startChange={this.startDateChange}
                endChange={this.endDateChange}
              />
            </div>
            <div style={{marginRight: 30}}>
              产品名称：
              <Input onChange={() => {
              }} placeholder="请输入名称" style={{width: 250}}/>
            </div>
            <div style={{marginRight: 30}}>
              生效状态：
              <Radio.Group options={options} onChange={this.onChange} value={this.state.status}/>
              {/*<Checkbox.Group options={options} onChange={this.onChange} />*/}
            </div>
            <div style={{marginRight: 30}}>
              <Button style={{width: 80, padding: '0px 5px'}} type="primary" onClick={() => {
                this.priceAdjustReviewQuery()
              }}>查询</Button>
            </div>
          </div>
        </Card>
        <Card title={<div><Icon type="minus-square" theme="twoTone" style={{marginRight: 5}}/>查询条件</div>}
              bordered={true} size='small'
              bodyStyle={{padding: 20}}
        >
          <div style={{marginTop: -10, marginBottom: 10}}>
            <Button type="link" style={{color: "black", padding: 0}} onClick={() => {
              this.priceEffectHandleMass()
            }}>
              <Icon type="snippets" style={{color: "rgb(250, 140, 23)"}}/>批量生效
            </Button>
          </div>
          <PubilcTablePagination
            param={{
              page,
              total,
              loading,
              data: tableData, // 表格数据
              x: totalWidth, // 表格的宽度
              y: store.getState().tableHeight.y - 50,
              componentName: 'PriceAdjustBillEffect', // 待处理表头
              height: store.getState().tableHeight.y,
              columns: columns && columns.length > 0 ? columns : this.staticColumns, // 表头配置
            }}
            compilePage={this.compilePage}
            getColumns={this.getTableColumns}
            onRef={ref => this.commonTableRef = ref}
            rowSelectionPublic={detailRowSelection}
            setRowClassNamePublic={this.setRowClassNamePublic}
          />
        </Card>
      </div>
    );
  }
}

export default PriceAdjustBillEffect;